﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using kim.Common;
using System.Data;

namespace kim
{
    partial class AddorEditLinks : kim.UI.ManagePage
    {

        protected string type;
        protected int typeid, fid;
        private int Nid;
        protected void Page_Load(object sender, EventArgs e)
        {
            type = KIMRequest.GetString("type");
            typeid = KIMRequest.GetInt("typeid", 0);
            Nid = KIMRequest.GetInt("id", 1);
            fid = KIMRequest.GetInt("fid", 1);
            if (!IsPostBack)
                GetRequestValues();
            GetLabl();
        }
        private void GetLabl()
        {
            Label1.Text = "友情链接";
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                if (DoEdit(Nid))
                    lbmsg.Text = "修改成功";
                else
                    lbmsg.Text = "修改失败";
            }
            if (type == KIMEnums.ActionEnum.Add.ToString())
            {
                if (DoAdd())
                    lbmsg.Text = "添加成功";
                else
                    lbmsg.Text = "添加失败";
            }

        }

        #region 编辑添加基本操作=========================================

        private bool DoEdit(int id)
        {
            Model.link Mnew = new Model.link();
            Mnew.sort_id = int.Parse(txt_postion.Value.Trim());
            Mnew.typeid = typeid;
            Mnew.ChannelID = fid;
            Mnew.title = txtTitle.Value;
            Mnew.id = id;
            Mnew.site_url = txtsite_url.Value;
            Headpic.Src = txtImgUrl_min.Value;
            return new BLL.link().Update(Mnew);
        }

        private bool DoAdd()
        {
            Model.link Mnew = new Model.link();
            Mnew.sort_id = int.Parse(txt_postion.Value.Trim());
            Mnew.typeid = typeid;
            Mnew.ChannelID = fid;
            Mnew.title = txtTitle.Value;
            Headpic.Src = txtImgUrl_min.Value;
            Mnew.site_url = txtsite_url.Value;
            BLL.link bll = new BLL.link();
            if (bll.Add(Mnew) > 0)
                return true;
            else
                return false;
        }
        #endregion

        private void GetRequestValues()
        {

            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                GetEditValue();
            }
            if (type == KIMEnums.ActionEnum.Delete.ToString())
            {
                bool del = new BLL.link().Delete(Nid);
                if (del)
                    Response.Redirect("List.aspx?typeid=" + typeid + "&fid=" + fid);
            }
            if (type != KIMEnums.ActionEnum.Edit.ToString() && type != KIMEnums.ActionEnum.Add.ToString() && type != KIMEnums.ActionEnum.Delete.ToString())
                Response.Redirect("../indexpage.aspx");
            else
            {
                //lb_po.Text = new BLL.eg_news().GetMaxSort("").ToString();
            }
        }

        private void GetEditValue()
        {
            Model.link model = new BLL.link().GetModel(Nid);
            this.txtTitle.Value = model.title;
            this.txt_postion.Value = model.sort_id.ToString();
            this.txtsite_url.Value = model.site_url;
        }
    }
}